﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using DONGACS.Common;
using DONGACS.Domain;

namespace DONGACS.DAO
{
    public class PathsDao : AccessData
    {
        //lấy tất cả Paths đang IsEnable
        public IList<Paths> GetAllPaths()
        {
            CommandText = "Select * From TB_Paths P Where P.IsDelete = 0 and P.IsEnable = 1 ";
            Commandtype = CommandType.Text;

            DataTable table = GetDataTable();

            IList<Paths> list = CollectionHelper.ConvertTo<Paths>(table);
            return list;
        }


        //Lấy Paths theo ID
        public Paths GetPathsByID( string PathID)
        {
            try
            {
                CommandText = "Select * From TB_Paths P Where P.ID = " + PathID + " and P.IsDelete = 0 and P.IsEnable = 1";
                Commandtype = CommandType.Text;

                DataTable table = GetDataTable();

                Paths path = CollectionHelper.CreateItem<Paths>(table.Rows[0]);

                return path;
            }
            catch (Exception)
            {
                return null;
            }
            
        }

        /// <summary>
        ///  Lấy toàn bộ các Blocks có trong hệ thống chứa trong bảng Paths
        /// </summary>
        /// <returns>IList of object</returns>
        public IList<object> GetAllBlocksInPaths()
        {
            CommandText = "Select Distinct P.Blocks, P.OrderBlocks From TB_Paths P Where P.IsDelete = 0 and P.IsEnable = 1 Order By P.OrderBlocks";
            Commandtype = CommandType.Text;

            DataTable table = GetDataTable();

            //IList<object> list = CollectionHelper.ConvertTo<object>(table);
            IList<object> list = new List<object>();
            for (int i = 0; i < table.Rows.Count; i++)
            {
                list.Add(table.Rows[i][0].ToString());
            }

            return list;
        }
        
        // Lấy toàn bộ các Module có trong hệ thống chứa trong bảng Paths
        public IList<object> GetAllModulesInPaths()
        {
            CommandText = "Select Distinct P.Modules, P.OrderModules From TB_Paths P Where P.IsDelete = 0 and P.IsEnable = 1 Order By P.OrderModules ASC";
            Commandtype = CommandType.Text;

            DataTable table = GetDataTable();

            //IList<object> list = CollectionHelper.ConvertTo<object>(table);
            IList<object> list = new List<object>();
            for (int i = 0; i < table.Rows.Count; i++)
            {
                list.Add(table.Rows[i][0].ToString());
            }

            return list;
        }
        
        // Lấy toàn bộ các Module By Blocks có trong hệ thống chứa trong bảng Paths
        public IList<object> GetAllModulesInPathsByBlocks(string block) // block : thuộc tính Blocks trong bảng Paths
        {
            CommandText = "Select Distinct P.Modules, P.OrderModules From TB_Paths P Where P.Blocks = N'" + block + "' and P.IsDelete = 0 and P.IsEnable = 1 Order By P.OrderModules ASC";
            Commandtype = CommandType.Text;

            DataTable table = GetDataTable();

            //IList<object> list = CollectionHelper.ConvertTo<object>(table);
            IList<object> list = new List<object>();
            for (int i = 0; i < table.Rows.Count; i++)
            {
                list.Add(table.Rows[i][0].ToString());
            }

            return list;
        }

        public Paths GetPathsByModulesAndPermissionType(string module, string permissionType)
        {
            CommandText = "Select * From TB_Paths P Where P.Modules = N'" + module + " and P.PermissionType = " + permissionType + " and P.IsDelete = 0 and P.IsEnable = 1" ;
            Commandtype = CommandType.Text;

            DataTable table = GetDataTable();

            Paths paths = CollectionHelper.CreateItem<Paths>(table.Rows[0]);

            return paths;
        }

        public IList<Paths> GetAllPathsByModulesAndPermissionType(string module, string permissionType)
        {
            CommandText = "Select * From TB_Paths P Where P.Modules = N'" + module + "' and P.PermissionType = '" + permissionType + "' and P.IsDelete = 0 and P.IsEnable = 1";
            Commandtype = CommandType.Text;

            DataTable table = GetDataTable();

            IList<Paths> list = CollectionHelper.ConvertTo<Paths>(table);
            return list;
        }
    }
}
